import 'package:flutter/material.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

/*
 * 自定义按钮
*/
class LxButton extends StatelessWidget {
  // 按钮布局
  final ButtonStyleEnum buttonStyleEnum;

  // 按钮类型
  final ButtonTypeEnum buttonTypeEnum;

  // 按钮权重
  final ButtonWeightEnum buttonWeightEnum;

  // 按钮大小
  final ButtonSizeEnum buttonSizeEnum;

  // 按钮状态
  final ButtonStausEnum buttonStausEnum;

  // 按钮排列方式
  final ButtonAlignmentEnum buttonAlignmentEnum;

  // 自定义 按钮样式颜色
  final ButtonTypeColor? buttonTypeCustomColor;

  // 文本
  final String? title;

  // 事件
  final Function? onTap;

  // icon 左右分布
  final List<String>? icons;

  // icon 左右分布
  final List<Widget>? customIcons;

  // icons为一个时，单个icon的位置
  final bool iconOnLeft;

  // 自定义高度
  final double? height;

  // 自定义圆角
  final BorderRadius? borderRadius;

  // 自定义按钮文本
  final LxFontWeightEnum? fontWeightEnum;

  // 初始化
  const LxButton({
    this.title,
    this.icons,
    this.customIcons,
    this.onTap,
    this.height,
    this.borderRadius,
    this.fontWeightEnum,
    this.buttonStyleEnum = ButtonStyleEnum.text,
    this.buttonTypeEnum = ButtonTypeEnum.base,
    this.buttonWeightEnum = ButtonWeightEnum.primary,
    this.buttonSizeEnum = ButtonSizeEnum.medium,
    this.buttonStausEnum = ButtonStausEnum.normal,
    this.buttonAlignmentEnum = ButtonAlignmentEnum.adaptive,
    this.iconOnLeft = true,
    this.buttonTypeCustomColor,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    // 圆角
    BorderRadius br = borderRadius ??
        BorderRadius.all(Radius.circular(buttonSizeEnum.height / 2));
    // 颜色
    ButtonTypeColor baseColor = buttonTypeCustomColor ??
        ButtonTypeDefaultColor(
          buttonTypeEnum: buttonTypeEnum,
          buttonWeightEnum: buttonWeightEnum,
          buttonStausEnum: buttonStausEnum,
        );
    return ClipRRect(
      borderRadius: br,
      child: SizedBox(
        height: height ?? buttonSizeEnum.height,
        // TextButton 添加点击效果
        child: TextButton(
          onPressed: () {
            if (buttonStausEnum == ButtonStausEnum.normal) {
              FocusScope.of(context).requestFocus(FocusNode());
              debugPrint("【$title】点击");
              if (onTap != null) {
                onTap!();
              }
            }
          },
          style: _buttonStyle(br, baseColor),
          child: buttonAlignmentEnum == ButtonAlignmentEnum.adaptive
              ? IntrinsicWidth(
                  // IntrinsicWidth 宽度根据内容自适应
                  child: _buttonContentLayout(baseColor),
                )
              : _buttonContentLayout(baseColor),
        ),
      ),
    );
  }

  // 按钮内容布局
  _buttonContentLayout(ButtonTypeColor baseColor) {
    int iconLen = icons?.length ?? 0;
    int customIconLen = customIcons?.length ?? 0;
    Color imgColor = baseColor.textColor;
    return Padding(
      padding: EdgeInsets.only(
        left: buttonSizeEnum.leftPadding,
        right: buttonSizeEnum.rightPadding,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          customIconLen >= 1 && iconOnLeft
              ? customIcons![0]
              : iconLen >= 1 && iconOnLeft
                  ? SizedBox(
                      width: buttonSizeEnum.iconWH,
                      height: buttonSizeEnum.iconWH,
                      child: ColorFiltered(
                        colorFilter: ColorFilter.mode(
                          imgColor,
                          BlendMode.srcATop,
                        ),
                        child: Opacity(
                          opacity: imgColor.alpha / 255,
                          child: Image.asset(
                            icons![0],
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                    )
                  : Container(),
          Visibility(
            visible: (iconLen >= 1 || customIconLen >= 1) && iconOnLeft,
            child: SizedBox(
              width: buttonSizeEnum.iconSpace,
            ),
          ),
          Text(
            title ?? '',
            style: TextStyle(
              fontSize: fontWeightEnum?.size ?? buttonSizeEnum.fontSize,
              fontWeight:
                  fontWeightEnum?.fontWeight ?? buttonSizeEnum.fontWeight,
              color: baseColor.textColor,
            ),
          ),
          Visibility(
            visible: iconLen >= 2 ||
                (iconLen >= 1 && !iconOnLeft) ||
                customIconLen >= 2 ||
                (customIconLen >= 1 && !iconOnLeft),
            child: SizedBox(
              width: buttonSizeEnum.iconSpace,
            ),
          ),
          customIconLen >= 2 || (customIconLen >= 1 && !iconOnLeft)
              ? customIcons![customIconLen >= 2 ? 1 : 0]
              : iconLen >= 2 || (iconLen >= 1 && !iconOnLeft)
                  ? SizedBox(
                      width: buttonSizeEnum.iconWH,
                      height: buttonSizeEnum.height,
                      child: ColorFiltered(
                        colorFilter: ColorFilter.mode(
                          imgColor,
                          BlendMode.srcATop,
                        ),
                        child: Opacity(
                          opacity: imgColor.alpha / 255,
                          child: Image.asset(
                            icons![iconLen >= 2 ? 1 : 0],
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                    )
                  : Container(),
        ],
      ),
    );
  }

  // 按钮风格
  ButtonStyle _buttonStyle(BorderRadius br, ButtonTypeColor baseColor) {
    return ButtonStyle(
        // 间隙
        padding: MaterialStateProperty.all(const EdgeInsets.all(0)),
        backgroundColor: MaterialStateProperty.all(
          buttonTypeEnum == ButtonTypeEnum.base
              ? baseColor.backgroundColor
              : Colors.transparent,
        ),
        // 点击色
        overlayColor: MaterialStateProperty.resolveWith<Color>(
          (Set<MaterialState> states) {
            if (states.contains(MaterialState.pressed)) {
              // 点击时的颜色
              return buttonStausEnum == ButtonStausEnum.disable
                  ? Colors.transparent
                  : baseColor.clickColor;
            }
            // 默认颜色透明
            return Colors.transparent;
          },
        ),
        // 边框
        side: MaterialStateProperty.all(
          BorderSide(
            color: baseColor.borderColor,
            width: 1,
          ),
        ),
        // 圆角
        shape: MaterialStateProperty.all(
            RoundedRectangleBorder(borderRadius: br)));
  }
}
